//
//  CharacterDetailsViewController.swift
//  SkylowAPP
//
//  Created by 漫云 on 2024/9/21.
//

import UIKit
import SnapKit

class CharacterDetailsViewController: UIViewController {
    
    let selectChatView = UIView()
    ///details
    var detailsObj:Dictionary<String, Any>!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = UIColor.colorWith(hexString: "#333333")
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named: "ic_nav_more"), style: .plain, target: self, action: #selector(rightBtnClick))
        
        self.title = "Skylow"
        
        
        detailsObj = [
            "headImg": "bg1",
            "title": "Grojband RPG",
            "name": "@bandddd",
            "num": "124",
            "introduction": "🎸 || Grojband RPG|| ☠️ “Grojband follows Corey and his best friends Ammmy and John and Candy and, ojband follows Corey and his best friends Ammmy and John and Candy and, ojband follows Corey and his best friends Ammmy and John and Candy and, ojband follows Corey and his best friends Ammmy and John and Candy and.",
        ]

        initWithUI()
    }
    // MARK: --- 添加
    @objc func addSelectChatButton() {
        selectChatView.backgroundColor = UIColor.colorWith(hexString: "#272727")
        selectChatView.addRoundedCorners(withRadius: 4)
        self.view.addSubview(selectChatView)
        
        
        let closeButton = UIButton()
        closeButton.setImage(UIImage(named: "character_close_icon"), for: .normal)
        closeButton.addTarget(self, action: #selector(clickPingMuShow), for: UIControl.Event.touchUpInside)
        selectChatView.addSubview(closeButton)
        closeButton.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(14)
            make.right.equalToSuperview().offset(-14)
            make.width.equalTo(24)
            make.height.equalTo(24)
        }
        
        let newChatButton = UIButton()
        newChatButton.setTitle("Chat in a new world", for: .normal)
        newChatButton.backgroundColor = UIColor.colorWith(hexString: "#D9D9D9")
        newChatButton.setTitleColor(.black, for: .normal)
//        newChatButton.titleLabel?.font = UIFont(name: "Inter", size: 12)
        newChatButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)
//        newChatButton.addTarget(self, action: #selector(clickNewChatButton), for: UIControl.Event.touchUpInside)
        selectChatView.addSubview(newChatButton)
        newChatButton.snp.makeConstraints { (make) in
            make.top.equalTo(closeButton.snp.bottom).offset(20)
            make.right.equalToSuperview().offset(-14)
            make.left.equalToSuperview().offset(14)
            make.height.equalTo(34)
        }
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.colorWith(hexString: "#D9D9D9")
        selectChatView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.top.equalTo(newChatButton.snp.bottom).offset(15)
            make.right.equalToSuperview().offset(-14)
            make.left.equalToSuperview().offset(14)
            make.height.equalTo(1)
        }
        
        let worldChatButton = UIButton()
        worldChatButton.setTitle("Chat in world sbaibxiad...", for: .normal)
        worldChatButton.backgroundColor = UIColor.colorWith(hexString: "#D9D9D9")
        worldChatButton.setTitleColor(.black, for: .normal)
//        worldChatButton.titleLabel?.font = UIFont(name: "Inter", size: 12)
        worldChatButton.titleLabel?.font = UIFont.systemFont(ofSize: 12)

//        newChatButton.addTarget(self, action: #selector(clickNewChatButton), for: UIControl.Event.touchUpInside)
        selectChatView.addSubview(worldChatButton)
        worldChatButton.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(15)
            make.right.equalToSuperview().offset(-14)
            make.left.equalToSuperview().offset(14)
            make.height.equalTo(34)
        }
        
        selectChatView.snp.makeConstraints { (make) in
            make.width.equalTo(217)
//            make.height.equalTo(285)
            make.bottom.equalTo(worldChatButton.snp.bottom).offset(40)
            make.center.equalToSuperview() // view1的中心和父视图一样
        }
    }
    
    
    // MARK: --- 关闭
    @objc func clickPingMuShow() {
       // 确保布局已经完成，然后删除子视图
        selectChatView.removeFromSuperview()
    }
    // MARK: --- Copy
    @objc func clickShareButton(){
        let link = "https://www.example.com"
        UIPasteboard.general.string = link
        showCopySuccessAlert()
    }
    // MARK: --- Copy Success
    func showCopySuccessAlert() {
          let alert = UIAlertController(title: "", message: "Copy Success", preferredStyle: .alert)
          alert.addAction(UIAlertAction(title: "Confirm", style: .default, handler: nil))
          self.present(alert, animated: true)
      }
    
    
    func initWithUI(){
        
        let totalBottomView = UIView()
        totalBottomView.backgroundColor = UIColor.black
        self.view.addSubview(totalBottomView)
       
        
        let headImgView = UIImageView(image: UIImage(named: detailsObj["headImg"] as! String))
        headImgView.addRoundedCorners(withRadius: 31)
        totalBottomView.addSubview(headImgView)
        
        headImgView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(24)
            make.width.equalTo(62)
            make.height.equalTo(62)
            make.centerX.equalToSuperview()
        }
     
        let titleLabel = UILabel()
        titleLabel.text = detailsObj["title"] as? String
        titleLabel.textColor = .white
        titleLabel.numberOfLines = 1
        titleLabel.font = .medium(14)
        titleLabel.textAlignment = .center
        totalBottomView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headImgView.snp.bottom).offset(17)
            make.width.lessThanOrEqualTo(200).priority(.required)
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
        }
        
        let hotImgView = UIImageView(image: UIImage(named: "hot"))
        totalBottomView.addSubview(hotImgView)
        hotImgView.snp.makeConstraints { (make) in
            make.top.equalTo(headImgView.snp.bottom).offset(20)
            make.left.equalTo(titleLabel.snp.right).offset(12)
            make.height.equalTo(13)
            make.width.equalTo(13)
        }
        
        let hotNumLabel = UILabel()
        hotNumLabel.text = detailsObj["num"] as? String
        hotNumLabel.textColor = .white
        hotNumLabel.numberOfLines = 1
        hotNumLabel.font = .regular(12)
        totalBottomView.addSubview(hotNumLabel)
        hotNumLabel.snp.makeConstraints { (make) in
            make.top.equalTo(headImgView.snp.bottom).offset(20)
            make.left.equalTo(hotImgView.snp.right).offset(2)
            make.height.equalTo(13)
            make.width.lessThanOrEqualTo(100).priority(.required)
        }
        
        let nameLabel = UILabel()
        nameLabel.text = detailsObj["name"] as? String
        nameLabel.textColor = .white
        nameLabel.numberOfLines = 1
        nameLabel.font = .regular(10)
        nameLabel.textAlignment = .center
        totalBottomView.addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(4)
            make.width.lessThanOrEqualTo(200).priority(.required)
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
        }
        
        let buttonView = UIView()
        totalBottomView.addSubview(buttonView)
        buttonView.snp.makeConstraints { (make) in
            make.top.equalTo(nameLabel.snp.bottom).offset(20)
            make.width.equalTo(280)
            make.height.equalTo(26)
            make.centerX.equalToSuperview()
        }
        
        let chatButton = UIButton()
        chatButton.setTitle("Chat", for: .normal)
        chatButton.addTarget(self, action: #selector(addSelectChatButton), for: UIControl.Event.touchUpInside)
        chatButton.titleLabel?.font =  .regular(12)
        chatButton.frame = CGRect(x: 10, y: 0, width: 60, height: 26)
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = chatButton.bounds
        gradientLayer.colors = [UIColor(red: 0.70, green: 0.27, blue: 0.57, alpha: 1.00).cgColor, UIColor(red: 0.95, green: 0.37, blue: 0.47, alpha: 1.00).cgColor] // 渐变色
        gradientLayer.locations = [0.0, 1.0] // 渐变点
        gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.8) // 起始点
        gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.8) // 结束点

        // 将渐变层添加到按钮图层并设置为背景
        chatButton.layer.insertSublayer(gradientLayer, at: 0)
        chatButton.addRoundedCorners(withRadius: 4)
        buttonView.addSubview(chatButton)
        
       
        let shareButton = UIButton()
        shareButton.setTitle("Share character profile", for: .normal)
        shareButton.titleLabel?.font = .regular(12)
        shareButton.addTarget(self, action: #selector(clickShareButton), for: UIControl.Event.touchUpInside)
        shareButton.frame = CGRect(x: 80, y: 0, width: 190, height: 26)
        let gradientLayerTwo = CAGradientLayer()
        gradientLayerTwo.frame = shareButton.bounds
        gradientLayerTwo.colors = [UIColor(red: 0.70, green: 0.27, blue: 0.57, alpha: 1.00).cgColor, UIColor(red: 0.95, green: 0.37, blue: 0.47, alpha: 1.00).cgColor] // 渐变色
        gradientLayerTwo.locations = [0.0, 1.0] // 渐变点
        gradientLayerTwo.startPoint = CGPoint(x: 0.0, y: 0.5) // 起始点
        gradientLayerTwo.endPoint = CGPoint(x: 1.0, y: 0.5) // 结束点

        // 将渐变层添加到按钮图层并设置为背景
        shareButton.layer.insertSublayer(gradientLayerTwo, at: 0)
        shareButton.addRoundedCorners(withRadius: 4)
        buttonView.addSubview(shareButton)
        

        let introductionLabel = UILabel()
        introductionLabel.text = detailsObj["introduction"] as? String
        introductionLabel.textColor = .white
        introductionLabel.numberOfLines = 0
        introductionLabel.font = .regular(10)
        introductionLabel.textAlignment = .center
        totalBottomView.addSubview(introductionLabel)
        introductionLabel.snp.makeConstraints { (make) in
            make.top.equalTo(buttonView.snp.bottom).offset(30)
            make.left.equalToSuperview().offset(40)
            make.right.equalToSuperview().offset(-40)
            make.centerX.equalToSuperview()
        }
        
        
        totalBottomView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(150)
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.bottom.equalTo(introductionLabel.snp.bottom).offset(40)
        }
        totalBottomView.layoutIfNeeded()
        totalBottomView.setNeedsLayout()
        
        totalBottomView.addRoundedCorners(withRadius: 300, byRoundingCorners: [.topLeft,.topRight])
    }
    
    
    @objc func rightBtnClick(){
       

    }
}
